<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class SwitchPoint extends Model
{
    protected $table = 'xfy_switch_points';
    protected $primaryKey = 'id';

    protected $fillable = ['switch_id', 'name', 'alias'];

    public function switches()
    {
        return $this->belongsTo(SwitchModel::class, 'switch_id');
    } 

    public static function addDefaultPoints($switchId){
        $data = [];
        $points = [
            'current' => '电流',
            'voltage' => '电压',
            'temp' => '温度',
            'power' => '功率',
            'status' => '状态',
            'electricQuantity' => '电量',
        ];
        foreach ($points as $alias => $name) {
            $one['switch_id'] = $switchId;
            $one['name'] = $name;
            $one['alias'] = $alias;
            $one['created_at'] = date("Y-m-d H:i:s");
            $one['updated_at'] = date("Y-m-d H:i:s");
            array_push($data, $one);
        }
        DB::table('xfy_switch_points')->insert($data);
    }
}